package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.nextinputs.Scheme;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.nextinputs.ValueScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.UserInfoActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.AreaBean;
import com.eqdd.yiqidian.bean.rebuild.UserCardInfoResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditDividerBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditIconBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.service.UserService;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.MultiChooseUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.Luban;

import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.ISelectAble;

public class UserInfoActivity extends SlimBaseMultiEditActivity {
    private UserInfoActivityCustom dataBinding;
    private List<AreaBean> areaBean;
    private BottomDialog areaDialog;
    private boolean photoChanged = false;
    private boolean nickNameChanged = false;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_info);
        initTopTitleBar(View.VISIBLE, "用户信息");
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (p) {
            case 1:
                PicUtil.singleSqare(this);
                break;
            case 4:
                startActivity(UserErweimaActivity.class);
                break;
            case 7:
                if (areaDialog == null) {
                    initAreaDialog();
                } else {
                    areaDialog.show();
                }
                break;
        }
    }

    private void initAreaDialog() {
        OkGo.<List<AreaBean>>post(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS + "0")
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .params("type", 0)
                .execute(new JsonCallBack<List<AreaBean>>() {
                    @Override
                    public void onSuccess(Response<List<AreaBean>> response) {
                        System.out.println("使用了网络请求");
                        createAreaDialog(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<List<AreaBean>> response) {
                        System.out.println("使用了缓存");
                        createAreaDialog(response);
                    }
                });
    }

    private void createAreaDialog(Response<List<AreaBean>> response) {
        areaBean = response.body();
        areaDialog = new MultiChooseUtil().getAreaDialog(UserInfoActivity.this, 2, areaBean, (d, a) -> {
            areaDialog.dismiss();
            String result = "";
            for (ISelectAble iSelectAble : a) {
                result += iSelectAble.getName();
            }
            String province = a.get(0).getName();
            String city = a.get(1).getName();
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_LOGIN_AREA)
                    .params("userGuid", user.getGuid())
                    .params("loginLocation", result)
                    .params("province", province)
                    .params("city", city)
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                photoChanged = false;
                                nickNameChanged = false;
                                setEditBeans();
                            }
                        }
                    });
        });
        areaDialog.show();
    }

    @Override
    protected void setEditBeans() {

        OkGo.<HttpResult<UserCardInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.USER_CARD_INFO)
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<UserCardInfoResultBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<UserCardInfoResultBean>> response) {
                        HttpResult<UserCardInfoResultBean> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            UserCardInfoResultBean items = httpResult.getItems();
                            datas.clear();
                            datas.add(new SlimEditDividerBean());
                            datas.add(new SlimEditIconBean("头像", items.getPhoto(), 1, true));
                            datas.add(new SlimEditEnterBean("昵称", items.getUpname(), 2, "请输入"));
                            datas.add(new SlimEditEnterBean("易企点号", items.getEQDCode(), 3, "请输入"));
                            datas.add(new SlimEditIconBean("二维码", "", 4, true, R.mipmap.erweima));
                            datas.add(new SlimEditDividerBean());
                            ArrayList<Scheme> schemes = new ArrayList<>();
                            schemes.add(StaticScheme.ChineseMobile());
                            datas.add(new SlimEditEnterBean("手机号", items.getUname(), 6, "请输入", schemes));
                            datas.add(new SlimEditEnterBean("地区", items.getLocation(), 7, true, "请选择"));
                            datas.add(new SlimEditDividerBean());
                            datas.add(new SlimEditHintBean("公司", items.getCompany(), 9));
                            datas.add(new SlimEditHintBean("部门/职位", TextUtils.isEmpty(items.getDepartment()) ? "" : (items.getDepartment() + "/" + items.getPost()), 10));
                            datas.add(new SlimEditDividerBean());
                            datas.add(new SlimEditEnterBean("个性签名", items.getSignature(), 12, "请输入").addSchemes(ValueScheme.MaxLength(30)).setTip("最多30个字"));
                            datas.add(new SlimEditDividerBean());
                            slimAdapterEx.updateData(datas);
                            if (photoChanged) {
                                RxBus.getDefault().send(1, items.getPhoto());
                            }
                            if (nickNameChanged) {
                                RxBus.getDefault().send(2, items.getUpname());

                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");

            switch (requestCode) {
                case 2:
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_NICKNAME)
                            .params("userGuid", user.getGuid())
                            .params("upname", content)
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        photoChanged = false;
                                        nickNameChanged = true;
                                        setEditBeans();
                                        startService(new Intent(UserInfoActivity.this, UserService.class));
                                    }
                                }
                            });
                    break;
                case 3:
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CHANGE_EQD_CODE)
                            .params("userGuid", user.getGuid())
                            .params("eqdCodeNew", content)
                            .execute(new DialogCallBack<HttpResult>(UserInfoActivity.this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        RxBus.getDefault().send(3, content);
                                        setEditBeans();
                                    }
                                }
                            });
                    break;
                case 6:
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_LOGIN_PHONE)
                            .params("userGuid", user.getGuid())
                            .params("uname", content)
                            .params("password", EncryptUtil.sha1(SPUtil.getParam(Config.PASS, "") + "EQD"))
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        photoChanged = false;
                                        nickNameChanged = false;
                                        setEditBeans();
                                        startService(new Intent(UserInfoActivity.this, UserService.class));

                                    }
                                }
                            });
                    break;
                case 12:
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_SIGNATURE)
                            .params("userGuid", user.getGuid())
                            .params("signature", content)
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        setEditBeans();
                                        startService(new Intent(UserInfoActivity.this, UserService.class));
                                    }
                                }
                            });
                    break;
            }
        }
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    LocalMedia localMedia = PictureSelector.obtainMultipleResult(data).get(0);
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_NICK_PHOTO)
                            .params("user", user.getUname())
                            .params("userGuid", user.getGuid())
                            .params("file", new File(localMedia.isCompressed() ? localMedia.getCompressPath() : localMedia.getPath()))
                            .execute(new DialogCallBack<HttpResult>(UserInfoActivity.this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());

                                    if (httpResult.getStatus() == 200) {
                                        photoChanged = true;
                                        nickNameChanged = false;
                                        setEditBeans();
                                        startService(new Intent(UserInfoActivity.this, UserService.class));
                                    }
                                }
                            });
                    break;
            }
        }
    }
}
